package processes;

import java.util.LinkedList;
import main.Lists;
import processes.*;
/**
 *
 * @author Ramunas
 */
public class ProcPrimitives {
    private static int id = 0;
    private static LinkedList<Process> list = new LinkedList<Process>();
    private static Process current;
    private static Process process;
    
    public static void addReady(Process process){
        list.add(process);
    }
    
    public static int getUniqueId(){
        id++;
        return id;
    }
    
    public static void createProcess(Process process, Process parent){
        if(parent != null){
            process.setParent(parent.getId());
            parent.addChildren(process.getId());
        }
        Lists.getProcesses().add(process);
        //processPlanner();
    }
    
    public static void destroyProcess(Process process){
        for(Integer child : process.getChildren()){
            Lists.getProcesses().remove(Lists.getProcessById(child));
        }
        process.getChildren().clear();
        process.getCreatedResources().clear();
        Lists.getProcesses().remove(process);       
        //processPlanner();
    }
    
    public static void stopProcess(Process process){
        String state = process.getState();
        if(state.equals("Vykdomas")){
            process.setState("Pasiruoses-sustabdytas");
        }else{
            process.setState(state.concat("-sustabdytas"));
        }
        //processPlanner();
    }
    
    public static void activateProcess(Process process){
        String state = process.getState();
        process.setState(state.replace("-sustabdytas", ""));
        //processPlanner();
    }
    
    public static boolean processPlanner(){
        if(!list.isEmpty()){
            process = list.removeFirst();
            process.setState("Vykdomas");
            process.go();
            return true;
        }else{
            return false;
        }

    }
}
